package com.luke.web.controller.base;

import java.util.List;

import com.luke.base.bo.DataCount;
import com.luke.base.domain.LkEquipment;
import com.luke.base.domain.LkVehicle;
import com.luke.base.service.ILkEquipmentService;
import com.luke.base.service.ILkVehicleService;
import com.luke.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.luke.common.annotation.Log;
import com.luke.common.core.controller.BaseController;
import com.luke.common.core.domain.AjaxResult;
import com.luke.common.enums.BusinessType;
import com.luke.base.domain.LkUser;
import com.luke.base.service.ILkUserService;
import com.luke.common.utils.poi.ExcelUtil;
import com.luke.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 * 
 * @author luke
 * @date 2021-05-27
 */
@RestController
@RequestMapping("/base/lkUser")
public class LkUserController extends BaseController
{
    @Autowired
    private ILkUserService lkUserService;

    @Autowired
    private ILkEquipmentService lkEquipmentService;

    @Autowired
    private ILkVehicleService vehicleService;

    /**
     * 查询用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('base:lkUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(LkUser lkUser)
    {
        startPage();
        lkUser.setParentId(0L);
        List<LkUser> list = lkUserService.selectLkUserList(lkUser);
        return getDataTable(list);
    }
    /**
     * 查询用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('base:lkUser:list')")
    @GetMapping("/lkUserList")
    public AjaxResult lkUserList(LkUser lkUser)
    {
        return AjaxResult.success(lkUserService.selectLkUserList(lkUser));
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('base:lkUser:export')")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LkUser lkUser)
    {
        List<LkUser> list = lkUserService.selectLkUserList(lkUser);
        ExcelUtil<LkUser> util = new ExcelUtil<LkUser>(LkUser.class);
        return util.exportExcel(list, "用户信息数据");
    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:lkUser:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        LkUser user=lkUserService.selectLkUserById(userId);
        //查询用户车辆车辆
        LkEquipment q=new LkEquipment();
        q.setUserId(userId);
        List<LkEquipment> equipmentList=lkEquipmentService.selectLkEquipmentList(q);
        user.setEquipmentList(equipmentList);
        return AjaxResult.success(user);
    }

    /**
     * 新增用户信息
     */
    @PreAuthorize("@ss.hasPermi('base:lkUser:add')")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LkUser lkUser)
    {
        return toAjax(lkUserService.insertLkUser(lkUser));
    }

    /**
     * 修改用户信息
     */
    @PreAuthorize("@ss.hasPermi('base:lkUser:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LkUser lkUser)
    {
        return toAjax(lkUserService.updateLkUser(lkUser));
    }

    /**
     * 删除用户信息
     */
    @PreAuthorize("@ss.hasPermi('base:lkUser:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(lkUserService.deleteLkUserByIds(userIds));
    }


    @GetMapping(value = "/dataCount")
    public AjaxResult dataCount()
    {
        AjaxResult result=AjaxResult.success();
        //用户统计
        DataCount userCount=lkUserService.getUserCount();
        //销量统计
        DataCount saleCount= lkEquipmentService.getSaleCount();
        //设备统计
        DataCount equipCount= lkEquipmentService.getEquipCount();
        //销冠车型
        DataCount vehicleCount= lkEquipmentService.getVehicleCount();
        //查询车辆图片
        if(StringUtils.isNotNull(vehicleCount)){
            LkVehicle vehicle= vehicleService.selectLkVehicleByModel(vehicleCount.getVehicleModel());
            if(StringUtils.isNotBlank(vehicle.getVehicleImage())){
                vehicleCount.setVehicleImage(vehicle.getVehicleImage().split(",")[0]);
            }
        }
        result.put("userCount",userCount);
        result.put("saleCount",saleCount);
        result.put("equipCount",equipCount);
        result.put("vehicleCount",vehicleCount);
        return result;
    }
}
